﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using predic.Web.Host.Services;
using predic.Web.Host.Model;
using System.ComponentModel;

namespace predic.Web2
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        private PredicContext ctx;

        public Preacher SelectedPreacher
        {
            get
            {
                return (Preacher)filterPredicator.SelectedItem ?? null;
            }
        }
        public ViewStudy SelectedViewStudy
        {
            get
            {
                return (ViewStudy)GridPredici.SelectedItem ?? null;
            }
        }

        public int SelectedPreacherID
        {
            get
            {
                if (SelectedPreacher != null)
                    return SelectedPreacher.ID;

                return -1;
            }
        }

        public string SelectedStudiu
        {
            get
            {
                return filterStudiu.Text;
            }
        }
        public string SelectedPredica
        {
            get
            {
                return filterPredica.Text;
            }
        }

        public MainPage()
        {
            InitializeComponent();

            ctx = new PredicContext();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            filterPredicator.SelectionChanged += new SelectionChangedEventHandler(filterPredicator_SelectionChanged);
            filterStudiu.SelectionChanged += new SelectionChangedEventHandler(filterStudiu_SelectionChanged);
            LoadFilterData();
        }

        void filterPredicator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // load studiu
            ctx.Load(ctx.GetDistinctTitlesByPreacherQuery(SelectedPreacherID),
                (load_operation_studiu) =>
                {
                    filterStudiu.ItemsSource = load_operation_studiu.Entities;

                    if (SelectedPreacherID > 0)
                    {
                        ctx.Load(ctx.GetDistinctSubtitlesByPreacherQuery(SelectedPreacherID),
                            (load_operation_predica) =>
                            {
                                filterPredica.ItemsSource = load_operation_predica.Entities;
                            }, null);
                    }
                }, null);

            // load predici
            if (SelectedPreacherID < 0)
            {
                ctx.Load(ctx.GetDistinctSubtitlesByPreacherQuery(SelectedPreacherID),
                    (load_operation_predica) =>
                    {
                        filterPredica.ItemsSource = load_operation_predica.Entities;
                    }, null);
            }

        }

        void filterStudiu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // load predica
            ctx.Load(ctx.GetDistinctSubtitlesByPreacherAndTitleQuery(SelectedPreacherID, SelectedStudiu),
                (load_operation_predica) =>
                {
                    filterPredica.ItemsSource = load_operation_predica.Entities;
                }, null);
        }

        private void LoadFilterData()
        {
            ctx.Load(ctx.GetPreachersQuery(),
                (loadOperation) =>
                {
                    filterPredicator.ItemsSource = loadOperation.Entities;
                }, null);
        }

        private void btnEditStudy_Click(object sender, RoutedEventArgs e)
        {
            Views.EditStudy editStudyWindow = new Views.EditStudy();
            editStudyWindow.SelectedViewStudy = SelectedViewStudy;
            editStudyWindow.Closed += new EventHandler(editStudyWindow_Closed);
            editStudyWindow.Show();
        }

        private void btnDeleteStudy_Click(object sender, RoutedEventArgs e)
        {

        }

        private void editStudyWindow_Closed(object sender, EventArgs e)
        {
            Views.EditStudy view = sender as Views.EditStudy;
            if (view.DialogResult == true)
            {
                StudiesView.Clear();
                StudiesView.Load();
            }
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            Filter();
        }

        private void Filter()
        {   
            //public IQueryable<ViewStudy> GetStudiesViewWithFilter(int preacherID, string title, string subtitle)
            if (this.SelectedPreacherID > 0)
            {
                StudiesView.QueryName = "GetStudiesViewWithFilter";

                StudiesView.QueryParameters.Clear();
                StudiesView.QueryParameters.Add(new Parameter() { ParameterName = "preacherID", Value = this.SelectedPreacherID });
                StudiesView.QueryParameters.Add(new Parameter() { ParameterName = "title", Value = this.SelectedStudiu });
                StudiesView.QueryParameters.Add(new Parameter() { ParameterName = "subtitle", Value = this.SelectedPredica });
            }
            else
            {
                StudiesView.QueryParameters.Clear();
                StudiesView.QueryName = "GetStudiesViewQuery";
            }

            StudiesView.Clear();
            StudiesView.Load();
        }

        private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.F)
            {
                Filter();
            }
            //else if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Alt) && e.Key == Key.N)
            //{

            //}
            //else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.E)
            //{

            //}
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyProperty(string property)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}
